<template>
	<el-container>
		<el-container>
            <el-header>
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'apply.add'">新增供应商</el-button>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
					<el-table-column type="selection"  align="center" width="50"></el-table-column>
					<el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
					<el-table-column label="供应商名称" prop="name" align="center"></el-table-column>
					<el-table-column label="供应商编码" prop="code" align="center"></el-table-column>
					<el-table-column label="联系人" prop="linkman" align="center"></el-table-column>
					<el-table-column label="联系电话" prop="mobile" align="center"></el-table-column>
					<el-table-column label="联系地址" prop="address" align="center"></el-table-column>
					<el-table-column label="禁用" prop="system_disable" align="center">
                        <template #default="scope">
                            <el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="1" :inactive-value="0"></el-switch>
						</template>
                    </el-table-column>
                    <el-table-column label="排序" prop="sort" align="center"></el-table-column>
					<el-table-column label="简介" prop="desc" align="center"></el-table-column>
					<el-table-column label="操作" align="center" width="180">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'apply.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'apply.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
	</el-container>
</template>

<script>
import saveDialog from './save'
export default {
	name: 'user',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			group: [],
			apiObj: this.$API.basicdata.applyList.getList,
			selection: [],
			search: {
				title: null,
				cid: null,
			},
		}
	},
	mounted() {
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
            console.log('row :>> ', row);
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        //表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.basicdata.applyList.update_status.post({
				id: row.id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.basicdata.applyList.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh();
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
            this.printArr = ''
		},
        selectionChange(selection) {
			this.selection = selection;
		},
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
.shopInfo{
    display: flex;
    align-items: center;
}
.img{
    width: 100px;
    height: 100px;
    margin-right: 5px;
}
</style>
